#define BOOST_TEST_MODULE MySuiteTest
#include <boost/test/included/unit_test.hpp>

#include <iostream>

#include "frreport_comm.h"
#include "frct_comm.h"
#include "frservice_comm.h"
#include "config.h"
#include "frmem_stat.h"
#include "frmem.h"
#include "pop3.h"

/*
*   pop3  私有信息
*/
typedef struct __st_L7session_pop3_private
{
    unsigned long ul_seq;                             //序列号，1次req和response时，使用同一个序列号
    L4_transaction st_l4_trans;                       //数据
    L7_transaction st_l7_trans;                       //数据
    int i_flag_data_type;                             //1: FRLOGIN_START 2:FRLOGIN_SUCC 3:FRLOGIN_FAIL 4 FRLOGOUT_START 5 FRLOGOUT_END 0 : other
    char *p_header_name;
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    char *p_user_name;
    struct timeval st_login_timestamp;
    char arr_session_id[APM_UUID_SIZE];
    int i_sign;                                       //标志位 0||1, 0:需要resp解析 1:不需要resp解析
    int i_flag_end;                                   //标志位 0||1, 0:req为PASS 1:req不为PASS
    int i_flag_response;                              //标志位 0||1, 0:response的code和string不必上报 1：需要上报
}L7session_pop3_private;

int __resp_parse(char *p_data, unsigned int ui_data_len, L7session_pop3_private *p_st_private, Frreport_body_data *p_st_body_data, Conntrack *p_st_conn);

//__resp_parse
/* test_suite1 start */
BOOST_AUTO_TEST_SUITE(test_suite1)
int i_ret = 0;
char *p_data = NULL;
unsigned int ui_data_len = 0;
L7session_pop3_private *p_st_private = NULL;
Frreport_body_data *p_st_body_data = NULL;
Conntrack *p_st_conn = NULL;
// test_case1 in test_suite1
BOOST_AUTO_TEST_CASE(test_case1)
{
    fr_slab_init(102400, "ut_test");
	i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);
	BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
}
// test_case2 in test_suite1
BOOST_AUTO_TEST_CASE(test_case2)
{
	unsigned int ui_data_len = 13;

	p_data = frstrdup("+OO 1 56519\r\n");
	assert(p_data);

	p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
	assert(p_st_private);
	memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
	p_st_private->i_sign = 0;
	p_st_private->i_flag_end = 0;
	p_st_private->i_flag_response = 1;
	p_st_private->i_flag_data_type = 0;

	p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
	assert(p_st_body_data);
	memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
	p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

	p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
	assert(p_st_conn);
	memset(p_st_conn, 0x00, sizeof(Conntrack));

	i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

	BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
	BOOST_REQUIRE_EQUAL(p_st_private->i_sign, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_end, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_response, 1);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_data_type, 0);

	frfree(p_data);
	pop3_session_destroy(p_st_private);
	frreport_body_free(&p_st_body_data);
	frfree(p_st_conn);
}
// test_case3 in test_suite1
BOOST_AUTO_TEST_CASE(test_case3)
{
	ui_data_len = 13;

	p_data = frstrdup("+OK 1 56519\r\n");
	assert(p_data);

	p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
	assert(p_st_private);
	memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
	p_st_private->i_sign = 0;
	p_st_private->i_flag_end = 0;
	p_st_private->i_flag_response = 1;
	p_st_private->i_flag_data_type = 0;

	p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
	assert(p_st_body_data);
	memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
	p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

	p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
	assert(p_st_conn);
	memset(p_st_conn, 0x00, sizeof(Conntrack));

	i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

	BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
	BOOST_REQUIRE_EQUAL(p_st_private->i_sign, 1);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_end, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_response, 1);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_data_type, 0);

	frfree(p_data);
	pop3_session_destroy(p_st_private);
	frreport_body_free(&p_st_body_data);
	frfree(p_st_conn);
}
// test_case4 in test_suite1
BOOST_AUTO_TEST_CASE(test_case4)
{
	ui_data_len = 29;

	p_data = frstrdup("+OK Name is a valid mailbox\r\n");
	assert(p_data);

	p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
	assert(p_st_private);
	memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
	p_st_private->i_sign = 0;
	p_st_private->i_flag_end = 0;
	p_st_private->i_flag_response = 0;
	p_st_private->i_flag_data_type = 1;

	p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
	assert(p_st_body_data);
	memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
	p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

	p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
	assert(p_st_conn);
	memset(p_st_conn, 0x00, sizeof(Conntrack));

	i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

	BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_TODO);
	BOOST_REQUIRE_EQUAL(p_st_private->i_sign, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_end, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_response, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_data_type, 1);

	frfree(p_data);
	pop3_session_destroy(p_st_private);
	frreport_body_free(&p_st_body_data);
	frfree(p_st_conn);
}
// test_case5 in test_suite1
BOOST_AUTO_TEST_CASE(test_case5)
{
	ui_data_len = 77;

	p_data = frstrdup("+OK Mailbox locked and ready SESSIONID=<mail.broada.com-24121-1427942444-1>\r\n");
	assert(p_data);

	p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
	assert(p_st_private);
	memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
	p_st_private->i_sign = 0;
	p_st_private->i_flag_end = 1;
	p_st_private->i_flag_response = 0;
	p_st_private->i_flag_data_type = 1;

	p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
	assert(p_st_body_data);
	memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
	p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

	p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
	assert(p_st_conn);
	memset(p_st_conn, 0x00, sizeof(Conntrack));

	i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

	BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
	BOOST_REQUIRE_EQUAL(p_st_private->i_sign, 1);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_end, 1);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_response, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_data_type, 2);

	frfree(p_data);
	pop3_session_destroy(p_st_private);
	frreport_body_free(&p_st_body_data);
	frfree(p_st_conn);
}
// test_case6 in test_suite1
BOOST_AUTO_TEST_CASE(test_case6)
{
	ui_data_len = 38;

	p_data = frstrdup("+OK scan listing follows\r\n1 56519\r\n.\r\n");
	assert(p_data);

	p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
	assert(p_st_private);
	memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
	p_st_private->i_sign = 0;
	p_st_private->i_flag_end = 0;
	p_st_private->i_flag_response = 0;
	p_st_private->i_flag_data_type = 0;

	p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
	assert(p_st_body_data);
	memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
	p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

	p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
	assert(p_st_conn);
	memset(p_st_conn, 0x00, sizeof(Conntrack));

	i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

	BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
	BOOST_REQUIRE_EQUAL(p_st_private->i_sign, 1);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_end, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_response, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_data_type, 0);

	frfree(p_data);
	pop3_session_destroy(p_st_private);
	frreport_body_free(&p_st_body_data);
	frfree(p_st_conn);
}
// test_case7 in test_suite1
BOOST_AUTO_TEST_CASE(test_case7)
{
	ui_data_len = 5;

	p_data = frstrdup("+OK\r\n");
	assert(p_data);

	p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
	assert(p_st_private);
	memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
	p_st_private->i_sign = 0;
	p_st_private->i_flag_end = 0;
	p_st_private->i_flag_response = 0;
	p_st_private->i_flag_data_type = 0;

	p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
	assert(p_st_body_data);
	memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
	p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

	p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
	assert(p_st_conn);
	memset(p_st_conn, 0x00, sizeof(Conntrack));

	i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

	BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
	BOOST_REQUIRE_EQUAL(p_st_private->i_sign, 1);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_end, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_response, 0);
	BOOST_REQUIRE_EQUAL(p_st_private->i_flag_data_type, 5);

	frfree(p_data);
	pop3_session_destroy(p_st_private);
	frreport_body_free(&p_st_body_data);
	frfree(p_st_conn);

	fr_slab_destroy();
}
BOOST_AUTO_TEST_SUITE_END()
/* test_suite1 end */